using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using CStock;
using CUtil;
using CapitalReSources;

namespace CGrid
{
	/// <summary>
	/// Summary description for GridBase.
	/// </summary>
	public class GridTrade : GridBase, IGrid
	{


		protected IMainClass mainClass;



		#region constructor

		public GridTrade():base()
		{
			
			try
			{

				this.mainClass = MainClass.Instance;
				this.mainClass.GridTrade = this;
				//this.CreateGridFromXMLFile("CapitalReSources.GridTemplates.GridTrade.xml");
			}
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error in creating GridTrade: " + ex.Message);
            }
			
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			
			base.Dispose( disposing );
		}
		#endregion

		#region IGrid

		public void WritePrognosisInListForIndex(ITradeStep tradeStep)
		{

			
//			//if myTradeSystemIndex = 0
//			//then write the first time
//			//else - only update one column
//		
//			if(tradeStep.TradeSystemIndex == 0 )
//			{
//				for(int i = 0; i < this.listViewItemPrognosis.Count; i ++)
//				{
//					this.listViewItemPrognosis[i] = string.Empty;
//				}
//
//				this.listViewItemPrognosis[PrognosisListViewEnum.Share.ToString()] = this.programContext.TradeEconomy.Portfolios[0].DataItem_Selected.DataClass.Name;
//				this.listViewItemPrognosis[PrognosisListViewEnum.Date.ToString()] = tradeStep.Date.ToShortDateString();
//				this.listViewItemPrognosis[PrognosisListViewEnum.Quote.ToString()] = tradeStep.ValueTotal;
//				this.listViewItemPrognosis[PrognosisListViewEnum.Indicator1.ToString()] = tradeStep.Prognosis;
//			
//				WritePrognosisInListView();
//			}
//			else
//			{
//				Debug.Assert(this.Items.Count > 0);
//				
//				if(this.Items.Count < 1)
//					return;
//
//				ListViewItem lvi = this.Items[TradeStep.DataItem.DataClassIndex];
//				System.Diagnostics.Debug.Assert(lvi != null,"Error getting list view item for displaying multiple simulation results");
//				
//				//Debug.WriteLine("Removing ListViewItem " + myDataClassIndex + " - text : " + lvi.Text);
//				
//
//				int i;
//				for(i = 0; i < lvi.SubItems.Count; i ++)
//				{
//					this.listViewItemPrognosis[i] = lvi.SubItems[i].Text;
//				}
//				i = tradeStep.TradeSystemIndex + 1;
//				string str = "Indicator"+ i.ToString();
//				this.listViewItemPrognosis[str] = tradeStep.Prognosis.ToString();
//				
//				WritePrognosisInListView();
//				
//
//			}
			

		}
		public void WriteSingleShareAllPrognosisInList(System.Collections.ArrayList tradeSystems,ITradeStep tradeStep)
		{


			//todo
//			//emtpy listview
//			for(int i = 0; i < this.listViewItemPrognosis.Count; i ++)
//			{
//				this.listViewItemPrognosis[i] = string.Empty;
//				
//			}
//
//			this.listViewItemPrognosis[PrognosisListViewEnum.Share.ToString()] = this.programContext.TradeEconomy.Portfolios[0].DataItem_Selected.DataClass.Name;
//			this.listViewItemPrognosis[PrognosisListViewEnum.Date.ToString()] = tradeStep.Date.ToShortDateString();
//			this.listViewItemPrognosis[PrognosisListViewEnum.Quote.ToString()] = tradeStep.ValueTotal;
//			
//		
//
//			//refresh column headers
//			if(tradeStep.PrognosisAll != null && tradeStep.PrognosisAll.Count >= tradeSystems.Count)
//			{
//				for(int i = 0; i < tradeSystems.Count; i++)
//				{
//					//				this.listViewItemPrognosis[PrognosisListViewEnum.Indicator1.ToString()] = this.tradeSystems[i].ToString();
//					//				this.listViewItemPrognosis[PrognosisListViewEnum.Indicator1.ToString()] = this.tradeSystems[i].ToString();
//
//					this.ListViewItemPrognosis[i + 3] = tradeStep.PrognosisAll[i].ToString();
//				}
//			}
//
//			//update listview columns
//			for(int i = 0; i < tradeSystems.Count; i++)
//			{
//				TradeSystem ts = tradeSystems[i] as TradeSystem;
//				Debug.Assert(ts != null);
//				if(this.Columns.Count > i + 3)
//					this.Columns[i + 3].Text = ts.Name;
//			}
//		
//			WritePrognosisInListView();

		}
		

//		public void SetPrognosisGrid()
//		{
//			this.ClearGridComplete();
//			this.CreateGridFromXMLFile("CapitalReSources.GridTemplates.GridPrognosis.xml");
//
//			
//		}
//		public void SetTradeGrid()
//		{
//			this.ClearGridComplete();
//			this.CreateGridFromXMLFile("CapitalReSources.GridTemplates.GridTrade.xml");
//
//			
//		}
//		public void SetOptimizationGrid()
//		{
//			this.ClearGridComplete();
//			this.CreateGridFromXMLFile("CapitalReSources.GridTemplates.GridOptimization.xml");
//
//			
//		}
//		public void SetTradeEventGrid()
//		{
//			this.ClearGridComplete();
//			this.CreateGridFromXMLFile("CapitalReSources.GridTemplates.GridTradeEvent.xml");
//	
//		}

		public void WriteShareIndicatorItems(NameObjectCollection collArr,ITradeStep tradeStep)
		{

//			//emtpy listview
//			for(int i = 0; i < this.listViewItemPrognosis.Count; i ++)
//			{
//				this.listViewItemPrognosis[i] = string.Empty;
//			}
//			
//			this.listViewItemPrognosis[PrognosisListViewEnum.Share.ToString()] = this.programContext.TradeEconomy.Portfolios[0].DataItem_Selected.DataClass.Name;
//			this.listViewItemPrognosis[PrognosisListViewEnum.Date.ToString()] = tradeStep.Date.ToShortDateString();
//			this.listViewItemPrognosis[PrognosisListViewEnum.Quote.ToString()] = tradeStep.QuoteShare;
//			
//		
//		
//			for(int i = 0; i < collArr.Count; i++)
//			{
//				this.Columns[i + 3].Text = collArr.Keys[i].ToString();
//				this.ListViewItemPrognosis[i + 3] = collArr[i].ToString();
//			}
//	
//
//		
//			WritePrognosisInListView();

		}
		public void WriteAllSharePrognosisInList(ITradeStep tradeStep)
		{

//			if(tradeStep.TradeSystemIndex == 0 )
//			{
//				for(int i = 0; i < this.listViewItemPrognosis.Count; i ++)
//				{
//					this.listViewItemPrognosis[i] = string.Empty;
//				}
//
//				this.listViewItemPrognosis[PrognosisListViewEnum.Share.ToString()] = this.programContext.TradeEconomy.Portfolios[0].DataItem_Selected.DataClass.Name;
//				this.listViewItemPrognosis[PrognosisListViewEnum.Date.ToString()] = tradeStep.Date.ToShortDateString();
//				this.listViewItemPrognosis[PrognosisListViewEnum.Quote.ToString()] = tradeStep.ValueTotal;
//				this.listViewItemPrognosis[PrognosisListViewEnum.Indicator1.ToString()] = tradeStep.Prognosis;
//			
//				WritePrognosisInListView();
//			}
//			else
//			{
//				ListViewItem lvi = this.Items[TradeStep.DataItem.DataClassIndex];
//				System.Diagnostics.Debug.Assert(lvi != null,"Error getting list view item for displaying multiple simulation results");
//				
//				//Debug.WriteLine("Removing ListViewItem " + myDataClassIndex + " - text : " + lvi.Text);
//				
//
//				int i;
//				for(i = 0; i < lvi.SubItems.Count; i ++)
//				{
//					this.listViewItemPrognosis[i] = lvi.SubItems[i].Text;
//				}
//				i = tradeStep.TradeSystemIndex + 1;
//				string str = "Indicator"+ i.ToString();
//				this.listViewItemPrognosis[str] = tradeStep.Prognosis.ToString();
//				
//				WritePrognosisInListView();
//				
//
//			}
			

		}
	


		public void WritePrognosisInListView()
		{

//			string[] subitems = new String[this.listViewItemPrognosis.Count];
//			for(int i = 0; i < this.listViewItemPrognosis.Count; i ++)
//			{
//				subitems[i] = this.listViewItemPrognosis[i].ToString();
//
//			}
//			
//			ListViewItem lvi = new ListViewItem(subitems);
//
//			this.Items.Add(lvi);
//			lvi.EnsureVisible();
//
//			System.Windows.Forms.Application.DoEvents();
			

		}
//		public void WritePortfolioTradeInListView(ITradeStep ts)
//		{
//
////			try
////			{
////				listViewItemTrade[TradeListViewEnum.Date.ToString()] = ts.Date.ToShortDateString();
////				listViewItemTrade[TradeListViewEnum.No.ToString()] = ts.DateIndex.ToString();
////				listViewItemTrade[TradeListViewEnum.BuySell.ToString()] = "--";
////				this.listViewItemTrade[TradeListViewEnum.PortfolioValue.ToString()] = ts.ValueTotal.ToString();
////
////				this.listViewItemTrade[TradeListViewEnum.PerformancePerYear.ToString()]=  ts.PerformancePerYear.ToString();
////				this.listViewItemTrade[TradeListViewEnum.RelPerformance.ToString()] = ts.PerformanceRelativePerYear.ToString();
////
////				this.listViewItemTrade[TradeListViewEnum.Indicators.ToString()] = ts.Indicators.ToString();
////
////				//this.bMultipleAssertionError = false;
////
////				this.listViewItemTrade[TradeListViewEnum.Quote.ToString()] = ts.ValueTotal.ToString();
////					
////			
////
////				string[] subitems = new String[10];
////				subitems[0] = this.listViewItemTrade[TradeListViewEnum.No.ToString()].ToString();
////				subitems[1] = "Portfolio: " + ts.NameItem;
////				subitems[2] = this.listViewItemTrade[TradeListViewEnum.Date.ToString()].ToString();
////				subitems[3] = this.listViewItemTrade[TradeListViewEnum.BuySell.ToString()].ToString();
////				subitems[4] = this.listViewItemTrade[TradeListViewEnum.Quote.ToString()].ToString();
////				subitems[5] = this.listViewItemTrade[TradeListViewEnum.NoShares.ToString()].ToString();
////				subitems[6] = this.listViewItemTrade[TradeListViewEnum.PortfolioValue.ToString()].ToString();
////				subitems[7] = this.listViewItemTrade[TradeListViewEnum.PerformancePerYear.ToString()].ToString();
////				subitems[8] = this.listViewItemTrade[TradeListViewEnum.RelPerformance.ToString()].ToString();
////				subitems[9] = this.listViewItemTrade[TradeListViewEnum.Indicators.ToString()].ToString();
////				AddListViewItem(subitems);
////			
////			}
////			catch(Exception exc)
////			{
////				Debug.Assert(false,"Error writing result in list",exc.Message);
////			}
//		}
//	
//		public void WriteTradeInListView()
//		{
////			string optimizationString = "";
////			string[] subitems = new String[12];
////			 
////			subitems[0] = this.listViewItemTrade[TradeListViewEnum.No.ToString()].ToString();
////			subitems[1] = this.programContext.TradeEconomy.Portfolios[0].DataItem_Selected.DataClass.Name;
////			subitems[2] = this.listViewItemTrade[TradeListViewEnum.Date.ToString()].ToString();
////			subitems[3]= this.listViewItemTrade[TradeListViewEnum.BuySell.ToString()].ToString();
////			subitems[4] =this.listViewItemTrade[TradeListViewEnum.Quote.ToString()].ToString();
////			subitems[5]=this.listViewItemTrade[TradeListViewEnum.NoShares.ToString()].ToString();
////			subitems[6]=this.listViewItemTrade[TradeListViewEnum.PortfolioValue.ToString()].ToString();
////			subitems[7] =this.listViewItemTrade[TradeListViewEnum.PerformancePerYear.ToString()].ToString();
////			subitems[8] =this.listViewItemTrade[TradeListViewEnum.RelPerformance.ToString()].ToString();
////			subitems[9] =this.listViewItemTrade[TradeListViewEnum.Indicators.ToString()].ToString();
////
////			subitems[11] =this.listViewItemTrade[TradeListViewEnum.TradesSuccesfull.ToString()].ToString();
////
////
////
////			if(optimizationString != "")
////			{		
////				subitems[9] = optimizationString;
////
////			}
////			AddListViewItem(subitems);
//		
//		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool WriteShareBaseDataToListPrepare(ITradeStep tradeStep)
		{
//			listViewItemTrade[TradeListViewEnum.Date.ToString()] = tradeStep.DateIndex.ToString();
//			listViewItemTrade[TradeListViewEnum.No.ToString()] = tradeStep.DateIndex.ToString();
//			listViewItemTrade[TradeListViewEnum.BuySell.ToString()] = "--";
//			
//			//this.bMultipleAssertionError = false;
//
//			this.listViewItemTrade[TradeListViewEnum.Quote.ToString()] = tradeStep.ValueTotal.ToString();
		
			
			return true;
		}
	
	

		public void WriteTradeEvents(System.Collections.ArrayList myTradeEvents)
		{
//			string[] subitems = new String[this.listViewItemTradeEvent.Count];
//
//			for(int i = 0; i < myTradeEvents.Count; i++)
//			{
//				TradeEvent te = myTradeEvents[i] as TradeEvent;
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.No.ToString()] = i.ToString();
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.Date.ToString()] = te.Date.ToShortDateString();
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.Share.ToString()] =te.ShareName;
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.Quote.ToString()] = te.Quote.ToString();
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.Number.ToString()] = te.Number.ToString();
//				this.listViewItemTradeEvent[TradeEventsListViewEnum.Money.ToString()] = te.Money.ToString();
//			
//				
//				for(int j = 0; j < this.listViewItemTradeEvent.Count; j ++)
//				{
//					subitems[j] = this.listViewItemTradeEvent[j].ToString();
//
//				}
//			
//				ListViewItem lvi = new ListViewItem(subitems);
//
//				this.Items.Add(lvi);
//				lvi.EnsureVisible();
//
//				System.Windows.Forms.Application.DoEvents();
//				
//			}
		}
		public void WriteTradeEvents(bool simulationPortfolio)
		{

		
			
		}

		public void WritePortfolioOptimization(System.Collections.ArrayList MyResultCollection,int myNumberOfItems)
		{
//			Debug.Assert(MyResultCollection != null);
//			if(MyResultCollection == null)
//				return;
//
//			foreach(OptimizationResult optOverall in MyResultCollection)
//			{
//				optOverall.PerformanceRelative = optOverall.PerformanceRelative /myNumberOfItems;
//
//				//--------------------------
//				//write result in listView
//				string[] subitems = new String[10];
//			 
//				subitems[0] = this.ListViewItemTrade[TradeListViewEnum.No.ToString()].ToString();
//				subitems[1] ="All Shares";
//				subitems[2] = this.ListViewItemTrade[TradeListViewEnum.Date.ToString()].ToString()	;
//				//subitems[3]= this.tradeListViewItem.BuySell;
//				//subitems[4] =this.tradeListViewItem.Quote;
//				//subitems[5]=this.tradeListViewItem.NoShares;
//				//subitems[6]=this.tradeListViewItem.PortfolioValue;
//				//subitems[7] =this.tradeListViewItem.Performance;
//				subitems[8] = optOverall.PerformanceRelative.ToString();
//				string indic = "";
//				foreach(double f in optOverall.ThresholdCollection)
//				{
//					indic += "(" + f.ToString() + ")";
//				}
//				subitems[9] =indic;
//				ListViewItem lvi = new ListViewItem(subitems);
//
//				this.Items.Add(lvi);
//
//			}
		}


		#endregion

	

		#region methods

//		public void ShowPortfolio(IPortfolio port)
//		{
//			if(this.grid1.Rows.Count > 1)
//			{
//				this.grid1.Rows.RemoveRange(1,this.grid1.Rows.Count -1);
//			}
//			for(int i = 0; i < port.DataItems.Count; i++)
//			{
//				IDataItem di = port.DataItems[i];
//				this.AddRow_Single(di);
//			}
//
//		}

		#endregion

	
	}
}
